package com.asia5b.a5mall.middleware.logic.viewmodel.promotion

import androidx.lifecycle.MutableLiveData
import com.asia5b.a5mall.middleware.R
import com.asia5b.a5mall.middleware.base.SimpleNetParams
import com.asia5b.a5mall.middleware.base.SimpleViewModel
import com.asia5b.a5mall.middleware.model.base.ResultModel
import com.asia5b.a5mall.middleware.model.promotion.CouponHallModel
import com.asia5b.a5mall.middleware.utils.showMessage
import io.reactivex.functions.Consumer

/**
 * Created by zhangyuncai on 2020/1/16.
 */
class CouponHallActivityVM:SimpleViewModel() {

  private  var page = 1
  private  var sumPage = 1000

    /**
     * @Note   加载优惠券
     * @param  page  分页
     */
    fun getDatas(refresh: Int) {
        if (refresh <= 1) {
            page = 1
            sumPage = 1000
        }
        if (page > sumPage) { dismissLoadingBar()
            return
        }

        val observable = promotionApi.getAllCoupon(page)

        baseSubscribe(observable, SimpleNetParams(isShowLoading = (refresh==0)),Consumer { resultModel ->
            sumPage = resultModel.sumPage
            page = resultModel.page+1
//            providerView().getDatasResult(resultModel.data!!)
            getDatasResultLiveData.value=resultModel
        })
    }
    val getDatasResultLiveData= MutableLiveData<ResultModel<ArrayList<CouponHallModel>>>()
    fun getCoupon(id: Int) {
        baseSubscribe(memberApi.receiveCoupon("$id"), Consumer {result->
            showMessage(R.string.领取成功)
        })

    }
}